clear
set more off

*************************************************
*Individual Level Regressions
*************************************************

*Get data
use ${tempdir}/panelreg_inddata_30yr.dta, clear

*Variables for the relevant marriagemarket and childhood years
gen maryear = year2 - 10
gen chyear = year2 - 30

*Here, assign the state the match the panel controls as statefip, state of residence
*Change to bpl to match on birth place instead
gen fips = statefip

*Merge on the marriage market variables
gen year = maryear
sort fips year 
merge m:1 fips year using ${tempdir}/panel_controls.dta, keepusing(pcturban pcturb25 pctmale pctmale_wtyouth pctmale_wt1530 pctmfglab_ipums pctsvclab_ipums pctmfglabm_ipums pctmfglabf_ipums pctfarm_hhhd /*
	*/ pctfor pctforwt pctforwtf pctforwtm ginifarmsize_diff ginifarmsize_con pctrr15 pctrr40 logoccscore_nvrmar_2035) keep(3) nogen

*Merge on the childhood variables - just schooling for now
replace year = chyear
sort fips year
merge m:1 fips year using ${tempdir}/panel_controls.dta, keepusing(pctsch_wt520 pctsch_wtm520 pctsch_wtf520) keep(3) nogen

*Merge on current outside logoccscore
replace year = year2
sort fips year
merge m:1 fips year using ${tempdir}/panel_controls.dta, keepusing(logoccscore_nvrmar_3045) keep(3) nogen


replace pctrr15 = 0 if pctrr15 == .
replace pctrr40 = 0 if pctrr40 == .

*Create labels

label var pcturban "Urban"
label var pctmale "Percent Male"
label var pctmale_wtyouth "Pct Male Among White Youth"
label var pctsvclab_ipums "Pct Lab Force in Services"
label var pctmfglab_ipums "Pct Lab Force in Manufacturing"
label var pctmfglabm_ipums "Pct Male Lab Force in Mfg"
label var pctmfglabf_ipums "Pct Female Lab Force in Mfg"
label var pctfarm_hhhd "Pct Farmers Among HH Heads"
label var pctsch_wt520 "Pct of Whites 5-20 in School"
label var pctsch_wtm520 "Pct of White Males 5-20 in School"
label var pctsch_wtf520 "Pct of White Females 5-20 in School"
label var pctfor "Pct Foreign"
label var pctforwt "Pct Foreign Among Whites"
label var pctforwtf "Pct Foreign Among White Females"
label var pctforwtm "Pct Foreign Among White Males"
label var ginifarmsize_con "Gini of Farm Size"
label var pctrr15 "Land w/in 15 mi of RR"
label var pctrr40 "Land w/in 40 mi of RR"
label var logoccscore_nvrmar_2035 "Log Occscore Single Women 20-35"
label var logoccscore_nvrmar_3045 "Log Occscore Single Women 30-45"

foreach var in pcturban pcturb25 pctmale pctmale_wtyouth pctmale_wt1530 pctmfglab_ipums pctsvclab_ipums pctmfglabm_ipums pctmfglabf_ipums pctfarm_hhhd /*
	*/ pctfor pctforwt pctforwtf pctforwtm ginifarmsize_diff ginifarmsize_con pctrr15 pctrr40 pctsch_wt520 pctsch_wtm520 pctsch_wtf520 logoccscore_nvrmar_2035 logoccscore_nvrmar_3045{
	rename `var' `var'_stres
}

replace fips = bpl


*Merge on the marriage market variables
replace year = maryear
sort fips year 
merge m:1 fips year using ${tempdir}/panel_controls.dta, keepusing(pcturban pcturb25 pctmale pctmale_wtyouth pctmale_wt1530 pctmfglab_ipums pctsvclab_ipums pctmfglabm_ipums pctmfglabf_ipums pctfarm_hhhd /*
	*/ pctfor pctforwt pctforwtf pctforwtm ginifarmsize_diff ginifarmsize_con pctrr15 pctrr40 logoccscore_nvrmar_2035) keep(1 3) nogen

*Merge on the childhood variables - just schooling for now
replace year = chyear
sort fips year
merge m:1 fips year using ${tempdir}/panel_controls.dta, keepusing(pctsch_wt520 pctsch_wtm520 pctsch_wtf520) keep(1 3) nogen

*Merge on current outside logoccscore
replace year = year2
sort fips year
merge m:1 fips year using ${tempdir}/panel_controls.dta, keepusing(logoccscore_nvrmar_3045) keep(3) nogen


replace pctrr15 = 0 if pctrr15 == . & bpl < 100
replace pctrr40 = 0 if pctrr40 == . & bpl < 100

*Create labels

label var pcturban "Urban"
label var pctmale "Percent Male"
label var pctmale_wtyouth "Pct Male Among White Youth"
label var pctsvclab_ipums "Pct Lab Force in Services"
label var pctmfglab_ipums "Pct Lab Force in Manufacturing"
label var pctmfglabm_ipums "Pct Male Lab Force in Mfg"
label var pctmfglabf_ipums "Pct Female Lab Force in Mfg"
label var pctfarm_hhhd "Pct Farmers Among HH Heads"
label var pctsch_wt520 "Pct of Whites 5-20 in School"
label var pctsch_wtm520 "Pct of White Males 5-20 in School"
label var pctsch_wtf520 "Pct of White Females 5-20 in School"
label var pctfor "Pct Foreign"
label var pctforwt "Pct Foreign Among Whites"
label var pctforwtf "Pct Foreign Among White Females"
label var pctforwtm "Pct Foreign Among White Males"
label var ginifarmsize_con "Gini of Farm Size"
label var pctrr15 "Land w/in 15 mi of RR"
label var pctrr40 "Land w/in 40 mi of RR"
label var logoccscore_nvrmar_2035 "Lagged Log Occscore Single Women 20-35"
label var logoccscore_nvrmar_3045 "Current Log Occscore Single Women 30-45"

foreach var in pcturban pcturb25 pctmale pctmale_wtyouth pctmale_wt1530 pctmfglab_ipums pctsvclab_ipums pctmfglabm_ipums pctmfglabf_ipums pctfarm_hhhd /*
	*/ pctfor pctforwt pctforwtf pctforwtm ginifarmsize_diff ginifarmsize_con pctrr15 pctrr40 pctsch_wt520 pctsch_wtm520 pctsch_wtf520  logoccscore_nvrmar_2035 logoccscore_nvrmar_3045{
	rename `var' `var'_stbpl
}

* Regressions of ever married status
egen firstcode = group(sex first year)

gen int bpl2 = bpl
replace bpl2 = 9999 if bpl>100

gen byte foreign_born = bpl2>100 if bpl2~=.

sort year2 foreign_born

foreach var in pcturban pcturb25 pctmale pctmale_wtyouth pctmale_wt1530 pctmfglab_ipums pctsvclab_ipums pctmfglabm_ipums pctmfglabf_ipums pctfarm_hhhd /*
	*/ pctfor pctforwt pctforwtf pctforwtm ginifarmsize_diff ginifarmsize_con pctrr15 pctrr40 pctsch_wt520 pctsch_wtm520 pctsch_wtf520{
	by year2: egen hold = mean(`var'_stbpl)
	replace `var'_stbpl = hold if foreign_born == 1
	drop hold
	}

gen logoccscore_gap = logoccscore_father_spouse - logoccscore_father
gen abs_agegap = abs(agegap)

gen urb = urban==2 if urban!=.
label var urb "Urban dummy"


forvalues i = 1/4 {
	gen q`i' = qtl_score_wgt_full==`i'
	replace q`i'=. if qtl_score_wgt_full==.
}

foreach y in $yearlist {
		gen y`y' = year2==`y'
}

forvalues i=1/4 {
	foreach y in $yearlist {
		gen q`i'_`y' = q`i'*y`y'
	}
}

local dep_ever_married = 1
local dep_logoccscore_dadinlaw_temp = 2
local dep_logoccscore_husband_temp = 3
local dep_agegap = 4

local sttype_stres = 1
local sttype_stbpl = 2

local x_ever_married = 1
local x_logoccscore_dadinlaw_temp = 2
local x_logoccscore_husband_temp = 3
local x_agegap = 4


gen logoccscore_dadinlaw_temp = logoccscore_father_spouse 
gen logoccscore_husband_temp = log(husboccscore)
gen husb_parent_qtl = qtl_score_wgt_full_spouse

cap postclose results
postfile results sttype depvar spec control qtile year b se using "$outputdir/regression_table_data.dta", replace

local c_stres ""
local c_stbpl "i.foreign_born##i.year2"

foreach state in stres stbpl{
foreach var of varlist ever_married logoccscore_dadinlaw_temp {

* Ever married regressions -- no bpl controls
reg `var' $varlist1 i.age foreign_born if sex==2, cluster(firstcode)
gen spec1_state`sttype_`state''_depvar`x_`var'' = e(sample)

forvalues i=2/4 {
	*local b`i'_1880 = _b[q`i']
	foreach y in $yearlist {
		local b`i'_`y' = _b[q`i'_`y']
		local se`i'_`y' = _se[q`i'_`y']
	}
}

forvalues i=2/4 {
	foreach y in $yearlist {
		post results (`sttype_`state'') (`dep_`var'') (1) (1) (`i') (`y') (`b`i'_`y'') (`se`i'_`y'')
	}
}

* Ever married regressions -- no bpl controls
reg `var' $varlist2 i.age foreign_born if sex==2, cluster(firstcode)

forvalues i=2/4 {
	local b`i'_1880 = _b[q`i']
		local se`i'_1880 = _se[q`i']
	foreach y in $yearlistshort {
		local b`i'_`y' = _b[q`i'_`y']
		local se`i'_`y' = _se[q`i'_`y']
	}
}

forvalues i=2/4 {
	foreach y in $yearlist {
		post results (`sttype_`state'') (`dep_`var'') (1) (2) (`i') (`y') (`b`i'_`y'') (`se`i'_`y'')
	}
}


* Ever married regressions -- full bpl controls
reg `var' $varlist1 i.age i.bpl2##i.year2 if sex==2, cluster(firstcode)
gen spec2_state`sttype_`state''_depvar`x_`var'' = e(sample)

forvalues i=2/4 {
	*local b`i'_1880 = _b[q`i']
	foreach y in $yearlist {
		local b`i'_`y' = _b[q`i'_`y']
		local se`i'_`y' = _se[q`i'_`y']
	}
}

forvalues i=2/4 {
	foreach y in $yearlist {
		post results (`sttype_`state'') (`dep_`var'') (2) (1) (`i') (`y') (`b`i'_`y'') (`se`i'_`y'')
	}
}

* Ever married regressions -- full bpl controls
reg `var' $varlist2 i.age i.bpl2##i.year2 if sex==2, cluster(firstcode)

forvalues i=2/4 {
	local b`i'_1880 = _b[q`i']
		local se`i'_1880 = _se[q`i']
	foreach y in $yearlistshort {
		local b`i'_`y' = _b[q`i'_`y']
		local se`i'_`y' = _se[q`i'_`y']
	}
}

forvalues i=2/4 {
	foreach y in $yearlist {
		post results (`sttype_`state'') (`dep_`var'') (2) (2) (`i') (`y') (`b`i'_`y'') (`se`i'_`y'')
	}
}



}
}

postclose results



save "${tempdir}reg_data_table.dta", replace 

clear 
save "${tempdir}reg_nobs_table.dta", replace empty

local dep_ever_married = 1
local dep_logoccscore_dadinlaw_temp = 2

foreach state in 1 2   {
forvalues var =  1/2{
forvalues j = 1/2 {

use "${tempdir}reg_data_table.dta", clear

keep if spec`j'_state`state'_depvar`var' == 1

sort year2 first
by year2 first: gen nnames_female = _n == 1

if "`var'" == "2"  {
sort year2 spousefirst
by year2 spousefirst: gen nnames_male = _n == 1
	}
	
collapse (sum) nobs = spec`j'_state`state'_depvar`var' nnames_*, by(year2)	

gen sttype = `state' 
gen depvar = `var'
gen spec = `j'

rename year2 year 

append using "${tempdir}reg_nobs_table.dta"
save "${tempdir}reg_nobs_table.dta", replace 

}
}
}

merge 1:m sttype depvar spec year using "$outputdir/regression_table_data.dta", nogen 
save "$outputdir/regression_table_data.dta", replace

export excel using "$outputdir/Regression Results.xlsx", firstrow(var) replace


erase "${tempdir}reg_nobs_table.dta"
erase "${tempdir}reg_data_table.dta"
